﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace Kiosk
{
    /// <summary>
    /// Interaction logic for MentorDisplay.xaml
    /// </summary>
    public partial class MentorDisplay : Window
    {
        private DataSet dsMentorInfo;
        private DataSet dsMentorCourses;
        private int returnValQ1;
        private int returnValQ2;
        private Mentor selectedMentor;
        private string favoriteCourses = "";
        public Window previousPage { set; get; }
        private bool noMentorErrorDisplayed;
        public MentorDisplay()
        {
            InitializeComponent();
            displayMentorInfo();
            previousPage = sGlobals.browseMentors;
            noMentorErrorDisplayed = false;
        }

        private void displayMentorInfo()
        {
            dsMentorCourses = new DataSet();
            dsMentorInfo = new DataSet();
            favoriteCourses = "";
            string mentorQuery = "SELECT m.mentor_id, CONCAT_WS(' ', first_name, last_name) AS fullname, photo, degree_name, college, " +
                                        "additional_comments, job_type_text, location_text, degree_name, pathway_name " +
                                 "FROM mentor m " +
                                 "INNER JOIN lkup_pathway p ON p.pathway_id = m.pathway_id " +
                                 "INNER JOIN lkup_job_type jt ON m.job_type = jt.job_type_id " +
                                 "INNER JOIN lkup_location l ON m.location = l.location_id " +
                                 "INNER JOIN lkup_degree d ON m.degree = d.degree_id " +
                                 "WHERE m.mentor_id = '" + sGlobals.selectedMentorID + "'";
            string mentor2Query = "SELECT course_name " +
                                  "FROM lkup_courses " +
                                  "WHERE course_id = (SELECT benneficial_course1 FROM mentor WHERE mentor_id = '" + sGlobals.selectedMentorID + "') " +
                                  "OR course_id = (SELECT benneficial_course2 FROM mentor WHERE mentor_id = '" + sGlobals.selectedMentorID + "') " +
                                  "OR course_id = (SELECT benneficial_course3 FROM mentor WHERE mentor_id = '" + sGlobals.selectedMentorID + "')";
            try
            {
                dsMentorInfo = sGlobals.dbWrapper.ExecuteSQLStatement(mentorQuery, ref returnValQ1);
                dsMentorCourses = sGlobals.dbWrapper.ExecuteSQLStatement(mentor2Query, ref returnValQ2);
                if (returnValQ1 > 0)
                {
                    if (returnValQ2 > 0)
                    {
                        for (int i = 0; i < dsMentorCourses.Tables[0].Rows.Count; i++)
                        {
                            favoriteCourses += dsMentorCourses.Tables[0].Rows[i]["course_name"].ToString() + "\r\n";
                        }
                    }
                    if (((byte[])dsMentorInfo.Tables[0].Rows[0]["photo"]).Count() == 0)
                    {
                        selectedMentor = new Mentor(
                            dsMentorInfo.Tables[0].Rows[0]["fullname"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["pathway_name"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["degree_name"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["mentor_id"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["college"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["job_type_text"].ToString(),
                            favoriteCourses,
                            dsMentorInfo.Tables[0].Rows[0]["location_text"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["additional_comments"].ToString());
                    }
                    else
                    {
                        selectedMentor = new Mentor(
                            dsMentorInfo.Tables[0].Rows[0]["fullname"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["pathway_name"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["degree_name"].ToString(),
                            (byte[])dsMentorInfo.Tables[0].Rows[0]["photo"],
                            dsMentorInfo.Tables[0].Rows[0]["mentor_id"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["college"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["job_type_text"].ToString(),
                            favoriteCourses,
                            dsMentorInfo.Tables[0].Rows[0]["location_text"].ToString(),
                            dsMentorInfo.Tables[0].Rows[0]["additional_comments"].ToString());
                    }
                    Binding mybinding = new Binding("Name");
                    mybinding.Source = selectedMentor;
                    lblTitle.SetBinding(Label.ContentProperty, mybinding);
                    MentorDisplayBox.DataContext = selectedMentor;
                }
                else
                {
                    if (!noMentorErrorDisplayed && this.IsActive)
                    {
                        WarningMessage warning = new WarningMessage();
                        warning.lblMessage.Content = "There was an error retreiving this mentor. Please try again later.\r\nIf the problem continues contact your system administrator.";
                        warning.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                if (!sGlobals.hasLogout)
                {
                    WarningMessage warning = new WarningMessage();
                    warning.lblMessage.Content = "There was an error loading the page.\r\nIf the problem persists, please contact your system administrator";
                    warning.ShowDialog();
                }
            }
        }

        private void Window_IsVisibleChanged_1(object sender, DependencyPropertyChangedEventArgs e)
        {
            displayMentorInfo();
            if (previousPage is FavoriteMentors || previousPage is FilterMentors)
            {
                sGlobals.selectedAOSID = new List<string>();
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            noMentorErrorDisplayed = false;
            sGlobals.browseMentors.previousPage = sGlobals.mentorDisplay;
            previousPage.Show();
            this.Hide();
        }

        private void btnPathways_Click(object sender, RoutedEventArgs e)
        {
            sGlobals.pathways.Show();
            this.Hide();
        }

        private void btnFocusedAreas_Click(object sender, RoutedEventArgs e)
        {
            sGlobals.focusedAreasOfStudy.Show();
            this.Hide();
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            sGlobals.logout();
        }
    }
}
